package com.igoal.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.compass.annotations.Searchable;
import org.compass.annotations.SearchableId;
import org.compass.annotations.SearchableProperty;
import org.hibernate.annotations.Type;

@Entity
@Table(name="user_goal_todo_task")
@Searchable
public class UserGoalTask extends BaseObject implements Audit {
	private static final long serialVersionUID = 5946190483512710458L;
	
	private Long id;	
	private Integer index;
	private String taskName;
	private Integer percentMeasure;
	private Date deadLine;
	private Boolean finished;
	private Date createDate;
	private Date updateDate;
	private DataStatus status;
	
//	private UserGoal userGoal;
	private UserGoalPhase userGoalPhase;
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@SearchableId
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Column(name="task_index", nullable=false)
	public Integer getIndex() {
		return index;
	}

	public void setIndex(Integer index) {
		this.index = index;
	}
	
	@Column(name="task_name", nullable=false)
	public String getTaskName() {
		return taskName;
	}

	public void setTaskName(String taskName) {
		this.taskName = taskName;
	}

	@Column(name="percent_measure", nullable=true)
	public Integer getPercentMeasure() {
		return percentMeasure;
	}

	public void setPercentMeasure(Integer percentMeasure) {
		this.percentMeasure = percentMeasure;
	}

	@Column(name="dead_line", nullable=true)
	public Date getDeadLine() {
		return deadLine;
	}

	public void setDeadLine(Date deadLine) {
		this.deadLine = deadLine;
	}
	
	@Column(name="finished", nullable=false)
	@Type(type = "yes_no")
	public Boolean getFinished() {
		return finished;
	}

	public void setFinished(Boolean finished) {
		this.finished = finished;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable=false, name="cdate")
	public Date getCreateDate() {
		return createDate;
	}

	@Enumerated(EnumType.STRING)
	@Column(nullable=false, name="status")
	@SearchableProperty
	public DataStatus getStatus() {
		return status;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable=false, name="mdate")
	public Date getUpdateDate() {
		return updateDate;
	}

	public void setCreateDate(Date date) {
		this.createDate = date;
	}

	public void setStatus(DataStatus dataStatus) {
		this.status = dataStatus;
	}

	public void setUpdateDate(Date date) {
		this.updateDate = date;
	}
	
	@ManyToOne
	@JoinColumn(name="phase_id")
	public UserGoalPhase getUserGoalPhase() {
		return userGoalPhase;
	}

	public void setUserGoalPhase(UserGoalPhase userGoalPhase) {
		this.userGoalPhase = userGoalPhase;
	}

	@Transient
	public User getUser(){
		return userGoalPhase.getUser();
	}
	
	@Transient
	public Goal getGoal(){
		return userGoalPhase.getGoal();
	}

	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append(getUser().getUsername()).append(" defined task ").append(taskName).append(" for goal ").append(getGoal().getTitle());
		return sb.toString();
	}

	@Override
	public boolean equals(Object o) {
		if(o instanceof UserGoalTask){
			UserGoalTask that = (UserGoalTask) o;
			if(getGoal().equals(that.getGoal()) && getUser().equals(that.getUser()) && id.equals(that.id) && taskName.equals(that.taskName) && index.equals(that.index)){
				return true;
			}			
		}
		return false;
	}

	@Override
	public int hashCode() {
		return id.hashCode();
	}

}
